﻿import os
from pathlib import Path

import pyautogui

from Config import root_path


def test1():
    import cv2
    import os

    # 图片路径
    image_path = r'D:\code\PYTHON_CODE\热血三国\战场\imgs\Battle.jpg'

    # 检查文件是否存在
    if os.path.exists(image_path):
        print("文件存在，路径正确")
    else:
        print("文件不存在，请检查路径")

    # 尝试读取图片
    image = cv2.imread(image_path)
    if image is None:
        print("读取图片失败，请检查路径或文件格式")
    else:
        print("图片读取成功")


def test2():
    from PIL import Image

    image_path = r'D:\code\PYTHON_CODE\热血三国\战场\imgs\Battle.jpg'
    try:
        with Image.open(image_path) as img:
            img.verify()  # 验证文件是否损坏
        print("文件可以正常打开")
    except Exception as e:
        print("文件损坏或格式不受支持:", e)


def test3():
    from PIL import Image
    import numpy as np
    import cv2

    # 图片路径
    image_path = r'D:\code\PYTHON_CODE\热血三国\战场\imgs\Battle.jpg'

    # 使用 PIL 读取图片
    try:
        pil_image = Image.open(image_path)
        print("文件可以正常打开")

        # 将 PIL 图像转换为 OpenCV 格式
        opencv_image = np.array(pil_image)  # 转换为 NumPy 数组
        opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_RGB2BGR)  # 转换颜色通道（PIL 是 RGB，OpenCV 是 BGR）

        # 显示图片
        cv2.imshow('Image', opencv_image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    except Exception as e:
        print("文件损坏或格式不受支持:", e)


def test4():
    import sys
    print("系统默认编码:", sys.getdefaultencoding())


def test5():
    print(os.getcwd())


if __name__ == '__main__':
    # test1()
    # test2()
    # test3()
    # test4()
    test5()
